package com.oscar;

import com.oscar.helper.SettingsHelper;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ToggleButton;

public class fgm_settings extends Fragment {
	Button fgmsettings_btnLogout;
	Button fgmsettings_btnRefesh;
	ToggleButton fgmsettings_toggleNotification;
	ToggleButton fgmsettings_Notification_Vibrate;

	public fgm_settings() {
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_settings, container,
				false);
		fgmsettings_btnLogout = (Button) rootView.findViewById(R.id.fgmsettings_btnLogout);
		fgmsettings_btnRefesh = (Button) rootView.findViewById(R.id.fgmsettings_btnRefesh);
		fgmsettings_toggleNotification = (ToggleButton) rootView.findViewById(R.id.fgmsettings_toggleNotification);
		fgmsettings_Notification_Vibrate = (ToggleButton) rootView.findViewById(R.id.fgmsettings_toggleNotification_Vibrate);
		initData();
		fgmsettings_Notification_Vibrate.setOnClickListener(new View.OnClickListener() {
		    @Override
		    public void onClick(View v) {
		        if (fgmsettings_Notification_Vibrate.isChecked()) {
		        	SettingsHelper.setSettingsNotificationVibrate(getActivity(), true);
		        } else {
		        	SettingsHelper.setSettingsNotificationVibrate(getActivity(), false);
		        }
		    }
		});
		fgmsettings_toggleNotification.setOnClickListener(new View.OnClickListener() {
		    @Override
		    public void onClick(View v) {
		        if (fgmsettings_toggleNotification.isChecked()) {
		        	SettingsHelper.setSettingsNotification(getActivity(), true);
		        } else {
		        	SettingsHelper.setSettingsNotification(getActivity(), false);
		        	SettingsHelper.setSettingsNotificationVibrate(getActivity(), false);
		        	fgmsettings_Notification_Vibrate.setChecked(false);
		        }
		    }
		});
		fgmsettings_btnLogout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		        builder.setMessage("Do you want to logout application?")
		               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		                   public void onClick(DialogInterface dialog, int id) 
		                   {
		                	   	SettingsHelper.setUserNameStored(getActivity(), "");
		                	   	FragmentManager fragmentManager = getFragmentManager();
		                       	Fragment fragment = new LoginFragment();
		               			fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack("LOGIN").setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
		                   }
		               })
		               .setNegativeButton("No", new DialogInterface.OnClickListener() {
		                   public void onClick(DialogInterface dialog, int id) {
		                	   
		                   }
		               });
		        builder.create().show();
			}
		});
		fgmsettings_btnRefesh.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		        builder.setMessage("Do you want to refresh application?")
		               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		                   public void onClick(DialogInterface dialog, int id) {
		                   }
		               })
		               .setNegativeButton("No", new DialogInterface.OnClickListener() {
		                   public void onClick(DialogInterface dialog, int id) {
		                	   
		                   }
		               });
		        builder.create().show();
			}
		});
		return rootView;
	}
	private void initData() 
	{
		Boolean settings_Notification = SettingsHelper.getSettingsNotification(getActivity());
		Boolean settings_NotificationVibrate = SettingsHelper.getSettingsNotificationVibrate(getActivity());
		if(settings_Notification==true)
		{
			fgmsettings_toggleNotification.setChecked(true);
		}else
		{
			fgmsettings_toggleNotification.setChecked(false);
		}
		if(settings_NotificationVibrate==true)
		{
			fgmsettings_Notification_Vibrate.setChecked(true);
		}else
		{
			fgmsettings_Notification_Vibrate.setChecked(false);
		}
		
	}
}
